<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/23
 * Time: 15:26
 */

namespace app\adminapiv3\service;


class HostSoftware extends Base
{

    /**
     * @name 列表
     * @param $params
     * @param int $limit
     * @return array
     * @author wx
     * @date 2019/10/23 15:30
     */
    function list($params, $limit = 30)
    {
        $server = new \app\common\service\HostSoftware();
        $data = $server->list($params, $limit);

        return $data;
    }

    /**
     * @name 添加
     * @param $addData
     * @author wx
     * @date 2019/10/23 15:39
     */
    function add($addData)
    {
        //1. 验证重复
        if ($this->checkExist(['name' => $addData['name']])) {
            $this->setError($this->CODE_FAIL,'请勿添加重复的名称');
            return false;
        }

        //2. 添加
        $addData['status'] = 1;
        $addData['off_time'] = 0;
        $addData['ctime'] = time();
        $addData['on_time'] = strtotime($addData['on_time']);

        $server = new \app\common\service\HostSoftware();
        $server->add($addData);
    }

    /**
     * @name 检查重复
     * @param $condition
     * @return bool
     * @author wx
     * @date time
     */
    function checkExist($condition)
    {
        $server = new \app\common\service\HostSoftware();
        $info = $server->findData($condition);

        if(!$info){
            return false;
        }

        return true;
    }

    function detail($id)
    {
        $server = new \app\common\service\HostSoftware();
        $info = $server->findData(['id' => $id]);

        if(!$info){
            $this->setError($this->CODE_FAIL, '数据错误');
            return false;
        }

        $info['on_time'] = date('Y-m-d H:i:s', $info['on_time']);
        $info['off_time'] = date('Y-m-d H:i:s', $info['off_time']);

        unset($info['ctime']);

        return $info;
    }

    /**
     * @name 修改
     * @param $id
     * @param $updateData
     * @author wx
     * @date 2019/10/23 15:39
     */
    function modify($id, $updateData)
    {
        $server = new \app\common\service\HostSoftware();

        if($updateData['status'] == 2){//2已退役
            $updateData['off_time'] = time();
        }else{
            $updateData['off_time'] = 0;
        }

        $server->modify(['id' => $id], $updateData);
    }

    /**
     * @name 删除
     * @param $id
     * @author wx
     * @date 2019/10/23 15:40
     */
    function del($id)
    {
        $condition[] = array('id', 'in', $id);
        $server = new \app\common\service\HostSoftware();
        $server->del($condition);
    }

    function retire($id)
    {
        $updataData['status'] = 2;
        $updataData['off_time'] = time();

        $server = new \app\common\service\HostSoftware();
        $server->modify(['id' => $id], $updataData);
    }
}